namespace PickGold.Charting
{
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.Security.Permissions;
	using System.Web;

	[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public abstract class ChartElementCollection<T> : Collection<T>, IChartElement, IDisposable where T : ChartElement
	{
		private CommonElements _common;
		private IChartElement _parent;
		internal int _suspendUpdates;

		internal ChartElementCollection(IChartElement parent)
		{
			this._parent = parent;
		}

		protected override void ClearItems()
		{
			this.SuspendUpdates();
			while (base.Count > 0)
			{
				this.RemoveItem(0);
			}
			this.ResumeUpdates();
		}

		internal virtual void Deinitialize(T item)
		{
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				foreach (T local in this)
				{
					local.Dispose();
				}
			}
		}

		internal virtual void Initialize(T item)
		{
		}

		protected override void InsertItem(int index, T item)
		{
			this.Initialize(item);
			item.Parent = this;
			base.InsertItem(index, item);
			this.Invalidate();
		}

		public virtual void Invalidate()
		{
			if ((this._parent != null) && !this.IsSuspended)
			{
				this._parent.Invalidate();
			}
		}

		protected override void RemoveItem(int index)
		{
			this.Deinitialize(base[index]);
			T local = base[index];
			local.Parent = null;
			base.RemoveItem(index);
			this.Invalidate();
		}

		public virtual void ResumeUpdates()
		{
			if (this._suspendUpdates > 0)
			{
				this._suspendUpdates--;
			}
			if (this._suspendUpdates == 0)
			{
				this.Invalidate();
			}
		}

		protected override void SetItem(int index, T item)
		{
			this.Initialize(item);
			item.Parent = this;
			base.SetItem(index, item);
			this.Invalidate();
		}

		public virtual void SuspendUpdates()
		{
			this._suspendUpdates++;
		}

		void IChartElement.Invalidate()
		{
			this.Invalidate();
		}

		internal Chart Chart
		{
			get
			{
				if (this.Common != null)
				{
					return this.Common.Chart;
				}
				return null;
			}
		}

		internal CommonElements Common
		{
			get
			{
				if ((this._common == null) && (this._parent != null))
				{
					this._common = this._parent.Common;
				}
				return this._common;
			}
		}

		internal bool IsSuspended
		{
			get
			{
				return (this._suspendUpdates > 0);
			}
		}

		internal List<T> ItemList
		{
			get
			{
				return (base.Items as List<T>);
			}
		}

		internal IChartElement Parent
		{
			get
			{
				return this._parent;
			}
			set
			{
				this._parent = value;
				this.Invalidate();
			}
		}

		CommonElements IChartElement.Common
		{
			get
			{
				return this.Common;
			}
		}

		IChartElement IChartElement.Parent
		{
			get
			{
				return this.Parent;
			}
			set
			{
				this.Parent = value;
			}
		}
	}
}

